﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Util;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public abstract class BaseCountCtrlViewModel<T> : ViewModel
    {

        #region Variables


        //开始时间
        protected DateTime _startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0);
        //结束时间
        protected DateTime _endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);

        protected static T _instance;

        //符合查询条件的所有病人集合
        protected ObservableCollection<PatientLinkModel> _patientLinkModelList = new ObservableCollection<PatientLinkModel>();

        //左边用于显示统计的信息
        protected ObservableCollection<BaseCountModel> _disBaseCountModelList = new ObservableCollection<BaseCountModel>();

        protected ObservableCollection<BaseCountModel> _baseCountModelList = new ObservableCollection<BaseCountModel>();

        //右边用于显示的具体麻醉医生的所以病人信息
        protected List<PatientLinkModel> _disPatientLinkModelList = new List<PatientLinkModel>();

        //模糊查询左边的Name；
        protected string _selName = "";
        //月份查询
        private ObservableCollection<MonthModel> monthMethodList = new ObservableCollection<MonthModel>();
        private MonthModel _selectedMonthMethod;

        #endregion

        #region Constructor

        public BaseCountCtrlViewModel()
        {
            CreateMonthMondelList();
        }

        #endregion

        #region Properties
        public MonthModel SelectedMonthMethod
        {
            get
            {
                return _selectedMonthMethod;
            }

            set
            {
                _selectedMonthMethod = value;
                GetMonthStartDate();
                RaisePropertyChanged("SelectedMonthMethod");
            }
        }

        public ObservableCollection<PatientLinkModel> PatientLinkModelList
        {
            get
            {

                return _patientLinkModelList;
            }

            set
            {
                _patientLinkModelList = value;
                RaisePropertyChanged("PatientLinkModelList");
            }
        }
        public List<PatientLinkModel> DisPatientLinkModelList
        {
            get
            {
                return _disPatientLinkModelList;
            }

            set
            {
                _disPatientLinkModelList = value;
                RaisePropertyChanged("DisPatientLinkModelList");
            }
        }

        public ObservableCollection<BaseCountModel> BaseCountModelList
        {
            get
            {
                return _baseCountModelList;
            }

            set
            {
                _baseCountModelList = value;
                RaisePropertyChanged("BaseCountModelList");
            }
        }
        public DateTime StartDate
        {
            get
            {
                return _startDate;
            }

            set
            {
                _startDate = value;
                RaisePropertyChanged("StartDate");
            }
        }

        public DateTime EndDate
        {
            get
            {
                return _endDate;
            }

            set
            {
                _endDate = value;
                RaisePropertyChanged("EndDate");
            }
        }
        public ObservableCollection<MonthModel> MonthMethodList
        {
            get
            {
                return monthMethodList;
            }

            set
            {
                monthMethodList = value;
            }
        }


        public string SelName
        {
            get
            {
                return _selName;
            }

            set
            {
                _selName = value;
                RaisePropertyChanged("DisBaseCountModelList");
            }
        }

        public ObservableCollection<BaseCountModel> DisBaseCountModelList
        {
            get
            {
                _disBaseCountModelList.Clear();
                IEnumerable<BaseCountModel> dis = _baseCountModelList.Where(O => O.Name.Contains(SelName)).ToList();
                foreach (BaseCountModel bcm in dis) _disBaseCountModelList.Add(bcm);
                return _disBaseCountModelList;
            }

            set
            {
                _disBaseCountModelList = value;
                RaisePropertyChanged("DisBaseCountModelList");
            }
        }
        #endregion

        #region Event

        #endregion

        #region Private Methods


        #endregion

        #region Public/Protected Methods


        private void CreateMonthMondelList()
        {
            MonthMethodList.Add(new MonthModel("", 0));
            string[] strArr = { "一月", "二月", "三月", "四月", "五月", "半年", "七月", "八月", "九月", "十月", "十一月", "一年" };
            for (int i = 0; i < strArr.Length; i++)
            {
                MonthMethodList.Add(new MonthModel("近" + strArr[i], i + 1));
            }
        }
        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = Activator.CreateInstance<T>();
                }
                return _instance;
            }
        }



        private void GetMonthStartDate()
        {

            EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            if (DateTime.Now.Month - SelectedMonthMethod.Code < 0)
            {
                StartDate = new DateTime(DateTime.Now.Year - 1, 13 + (DateTime.Now.Month - SelectedMonthMethod.Code), 1, 0, 0, 0);
            }
            else
            {
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month - SelectedMonthMethod.Code + 1, 1, 0, 0, 0);
            }
        }


        abstract public void UpdateBaseCountModelList();

        public virtual void Seach()
        {
            PatientLinkModelList.Clear();
            BaseCountModelList.Clear();
            if (StartDate < EndDate)
            {
                IEnumerable<PatientLinkModel> PatientLinkModels = PatientLinkService.Instance.GetBaseCount(StartDate, EndDate).OrderByDescending(O => O.Status.InRoomTime);
                foreach (var item in PatientLinkModels)
                {
                    PatientLinkModelList.Add(item);
                    if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_SCDC))
                    {
                        WorkScoreUtil.CalculateScoreDetails(item);
                    }
                }
                UpdateBaseCountModelList();
                SelName = SelName;
            }
        }

        #endregion

        #region EventHandlers

        #endregion


        #region Command

        #region ChangeDoctorCommand


        #endregion

        #region OpenPopupCommand


        #endregion
        #endregion


    }
    public class MonthModel
    {

        private string name;
        private int code;


        public MonthModel(string name, int code)
        {
            this.name = name;
            this.code = code;
        }

        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public int Code
        {
            get
            {
                return code;
            }

            set
            {
                code = value;
            }
        }
    }
}
